package com.zh.web.controller.web.taxi;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.AopLog;
import com.zh.business.vo.web.DownTreeVo;
import com.zh.zhbootcommon.config.annotation.VerifyToken;
import com.zh.zhbootcommon.domain.DPage;
import com.zh.zhbootcommon.domain.DResult;
import com.zh.business.service.taxi.IDMiniUserInfoService;
import com.zh.zhbootcommon.entity.mp.MiniUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 小程序用户信息表 前端控制器
 * </p>
 *
 * @author JueDream
 * @since 2021-08-13
 */
@Slf4j
@VerifyToken
@RestController
@RequestMapping("/mini/v1")
@Api(value = "小程序用户信息")
@AopLog(type = "小程序用户信息", stackTraceOnErr = true)
public class DMiniUserInfoController {

    @Autowired
    IDMiniUserInfoService miniUserInfoService;

    @ApiOperation(value = "小程序用户列表查询接口")
    @PostMapping("/queryMiniUserPage")
    public DResult queryMiniUserPage(@RequestParam int page, @RequestParam int limit, @RequestParam int type, @RequestParam String mName){
        Page<MiniUserInfo> miniUserInfoPage = miniUserInfoService.queryMiniUserPage(page, limit, type, mName);
        DPage<MiniUserInfo> miniUserInfoDPage = DPage.to(miniUserInfoPage);
        return DResult.of(miniUserInfoDPage);
    }

    @ApiOperation(value = "小程序用户下拉列表查询接口")
    @GetMapping("/queryMiniUser")
    public DResult queryMiniUser(@RequestParam int type){
        List<MiniUserInfo> userList = miniUserInfoService.queryMiniUserAll(type);
        List<DownTreeVo> downTreeVos = new ArrayList<>();
        Optional.ofNullable(userList).orElse(new ArrayList<>())
                .forEach(a -> {
                    downTreeVos.add(DownTreeVo.builder().id(a.getMId())
                            .title(a.getOpenId() + "-" + a.getMName() ).build());
                });
        return DResult.of(downTreeVos);
    }

    @ApiOperation(value = "小程序用户列表查询单个接口")
    @PostMapping("/queryMiniUserOne")
    public DResult queryMiniUserOne(@RequestParam Integer mId){
        return DResult.of(miniUserInfoService.queryMiniUserOne(mId));
    }

    @Transactional
    @ApiOperation(value = "小程序用户列表编辑接口")
    @PostMapping("/saveOrUpdate")
    public DResult saveOrUpdate(@RequestBody MiniUserInfo miniUserInfo) {
        boolean flag = false;
        if (miniUserInfo.getMId() < 0) {
            // 新增
            flag = miniUserInfoService.save(miniUserInfo);
        } else if (miniUserInfo.getMId()  > 0) {
            // 更新
            flag = miniUserInfoService.updateById(miniUserInfo);
        }
        return DResult.of(flag);
    }

    @ApiOperation(value = "小程序用户删除接口")
    @DeleteMapping("/delete")
    public DResult delete(@RequestParam Integer mId){
        boolean flag = false;
        if (mId > 0) {
            flag = miniUserInfoService.removeMiniUser(mId);
        }
        return DResult.of(flag);
    }


}
